// pages/phone_login/phone_login.js
let App = getApp();
var countdown = 60;
var settime = function(that) {
    if (countdown == 0) {
        that.setData({
            is_show: true
        })
        countdown = 60;
        return;
    } else {
        that.setData({
            is_show: false,
            last_time: countdown
        })

        countdown--;
    }
    setTimeout(function() {
        settime(that)
    }, 1000)
}
Page({
    /**
     * 页面的初始数据
     */
    data: {
        last_time: '', //倒计时秒数
        is_show: true, //计时器开关
        phone: '', //电话号
        code: '' //验证码
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {},
    phoneInputBlur: function(e) {
        var phoneName = e.detail.value
        console.log(e.detail.value)
        this.setData({
            phone: phoneName
        })
        // wx.setStorageSync(key, data)
    },
    codeInputBlur: function(e) {
        var codes = e.detail.value;
        this.setData({
            code: codes
        })
    },
    // 获取验证码
    clickVerify: function (e) {
        var phone = this.data.phone;
        App._get('membership/index/code', {
            phone: phone
        }, function (result) {
            if (result.status == "1") {
                wx.showToast({
                    title: '发送成功',
                    icon: 'success',
                    duration: 2000,
                })
            } else {
                wx.showToast({
                    title: '手机号错误',
                    image: '../../image/icon_danger.png',
                    duration: 2000,
                })
            }
        })
        var that = this
        that.setData({
            is_show: (!that.data.is_show)
        })
        settime(that);
    },
    authorLogin: function (e) {
        console.log(e)
        var This = this;  
        // if (e.detail.errMsg !== 'getUserInfo:ok') {
        //     //用户按了拒绝按钮
        //     wx.showModal({
        //         title: '警告',
        //         content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        //         showCancel: false,
        //         confirmText: '返回授权',
        //         success: function (res) {
        //             if (res.confirm) {
        //                 console.log('用户点击了“返回授权”')
        //             }
        //         }
        //     })
        // }
        // wx.showLoading({
        //     title: "正在登录",
        //     mask: true
        // });
        // 执行微信登录
        wx.login({
            success: function (res) {
                // 发送用户信息
                App._post_form('coach/index/wxLogin', {
                    code: res.code,
                }, function (result) {
                    // 记录token user_id
                    console.log(result)
                    wx.setStorageSync('openid', result.list.openid);
                    App._get('coach/index/getUserinfo', {
                        openid: result.list.openid,
                        nickname: e.detail.userInfo.nickName,
                        gender: e.detail.userInfo.gender,
                        avatarUrl: e.detail.userInfo.avatarUrl,
                        encryptedData: e.detail.encryptedData,
                        iv: e.detail.iv,
                        city: e.detail.userInfo.city
                    }, function (result) {
                        console.log(result)
                    }, false, function () {

                    })
                    if (This.data.phone == '') {
                        wx.showToast({
                            title: ' 手机号错误 ',
                            image: '../../image/icon_danger.png',
                            duration: 2000,
                        })
                        return false;
                    }
                    if (This.data.code == '') {
                        wx.showToast({
                            title: '短信验证码有误',
                            image: '../../image/icon_danger.png',
                            duration: 2000,
                        })
                        return false;
                    }
                    App._get('membership/index/phoneLogin', {
                        phone: This.data.phone,
                        code: This.data.code,
                        // openid: "oNxK45Uk3QWRjTdpPeXNdeLd9X00",
                        openid: wx.getStorageSync('openid'),
                        type: 1
                    }, function (result) {
                        if (result.status == 1) {
                            wx.showToast({
                                title: '登陆成功',
                                icon: 'success',
                                duration: 2000,
                            })
                            wx.setStorageSync('imgUrl', result.list[0].img)
                            wx.setStorageSync('coachName', result.list[0].coach_name)
                            wx.setStorageSync('storeName', result.list[0].store_name)
                            setTimeout(function () {
                                wx.switchTab({
                                    url: "/pages/index/index"
                                });
                            }, 2000)
                            wx.setStorageSync('store', result.list);
                            wx.setStorageSync('phone', result.phone)
                            wx.setStorageSync('level', result.list[0].level)
                            wx.setStorageSync('store_id', result.list[0].id)
                        } else {
                            wx.showToast({
                                title: result.error,
                                icon: 'none',
                                // image: '../../image/icon_danger.png',
                                duration: 2000,
                            })
                        }
                    })
                }, false, function () {
                    wx.hideLoading();
                });

            }
        });  
    },
    phone_login (){
        var This = this;
        
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})